<template>
  <a-modal v-model="visible" title="所属用工单位" @ok="visible = false">
    <a-input-search v-model="term" placeholder="请输入所属用工单位" class="mv12" style="width: 200px" />
    <a-table :dataSource="dataSourceFilter" :columns="columns" :pagination="pagination" rowKey="name" @change="onTableChange" />
  </a-modal>
</template>
<script>
import { selectCompanyBySaleUserIdReq } from "../api";
import { getDate } from "@/utils/tools";
export default {
  name: "",
  components: {},
  mixins: [],
  props: {},
  data() {
    return {
      term: "",
      visible: false,
      dataSource: [],
      pagination: {
        pageSize: 10,
        total: 0,
        size: "default",
        current: 1,
        showTotal: (total) => `共 ${total} 条`,
        showQuickJumper: true,
        showSizeChanger: true,
        pageSizeOptions: ["10", "20", "50", "100"]
      },
      columns: [
        {
          title: "所属用工单位",
          dataIndex: "name"
        },
        {
          title: "绑定时间",
          dataIndex: "saleBindTime",
          customRender: (text) => {
            const date = new Date(text);
            return getDate(date, true);
          }
        }
      ]
    };
  },
  computed: {
    dataSourceFilter() {
      if (this.term) {
        return this.dataSource.filter((item) => item.name.includes(this.term));
      } else {
        return this.dataSource;
      }
    }
  },
  watch: {
    dataSourceFilter(newVal) {
      this.pagination.total = newVal.length;
    }
  },
  created() {},
  mounted() {},
  methods: {
    onTableChange(pagination) {
      this.pagination.current = pagination.current;
      this.pagination.pageSize = pagination.pageSize;
    },
    onItemClick(userId) {
      selectCompanyBySaleUserIdReq(userId)
        .then((res) => {
          this.term = "";
          this.dataSource = res.data;
          this.pagination.total = res.data.length;
          this.visible = true;
        })
        .catch((err) => {
          console.log("selectCompanyBySaleUserIdReq", err);
        });
    }
  }
};
</script>
<style lang="scss"></style>
